﻿using System;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls.Maps;
using Ninject;
using SydEnergiPrototype.DataAccess;
using SydEnergiPrototype.Logic;
using SydEnergiPrototype.Model;

namespace SydEnergiPrototype.ViewModel
{
    public class DisplayCabinetViewModel : ViewModelBase, IDisplayCabinetViewModel
    {
        private const int DefaultZoomLevel = 15;

        private int _zoomLevel;
        public int ZoomLevel
        {
            get { return _zoomLevel; }
            set
            {
                _zoomLevel = value; 
                Notify("ZoomLevel");
            }
        }

        private CredentialsProvider _credentials;
        public CredentialsProvider Credentials
        {
            get
            {
                if(_credentials == null)
                    _credentials = new ApplicationIdCredentialsProvider(App.Id);

                return _credentials;
            }
        }

        private Cabinet _currentCabinet;
        public Cabinet CurrentCabinet
        {
            get
            {
                if (_currentCabinet == null)
                {
                    _currentCabinet = _memoryDataStore.RetrieveCabinet();
                    _memoryDataStore.PersistCabinetId(_currentCabinet.Id);
                }
                return _currentCabinet;
            }
        }

        private ObservableCollection<PushPin> _pins; 
        public ObservableCollection<PushPin> Pins
        {
            get
            {
                if(_pins == null)
                    _pins = new ObservableCollection<PushPin> {new PushPin(CurrentCabinet.CabinetPosition, CurrentCabinet.Id)};

                return _pins;
            }
        }

        private SaveEntryToDataStoreCommand _saveEntryCommand;
        public SaveEntryToDataStoreCommand SaveEntryCommand
        {
            get
            {
                if(_saveEntryCommand == null)
                    _saveEntryCommand = new SaveEntryToDataStoreCommand();

                return _saveEntryCommand;
            }
        }

        private readonly IMemoryDataStore _memoryDataStore;

        [Inject]
        public DisplayCabinetViewModel(IMemoryDataStore memoryDataStore)
        {
            if(memoryDataStore == null)
                throw new ArgumentNullException("memoryDataStore");

            _memoryDataStore = memoryDataStore;

            ZoomLevel = DefaultZoomLevel;
        }
    }
}
